/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.PurchaseBillBean;
import com.minierp.bean.PurchaseBillItemBean;
import com.minierp.dao.PurchaseBillDao;
import com.minierp.model.PurchaseBill;
import com.minierp.model.PurchaseBillItem;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class PurchaseBillValidator implements Validator {
	private PurchaseBillDao purchaseBillDao;

	public boolean supports(Class clazz) {
		return PurchaseBillBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		PurchaseBillBean bean = (PurchaseBillBean) obj;
		PurchaseBill form = bean.getPurchaseBill();
		boolean hasError = false;
		if (form.getTallySetId() == 0L) {
			hasError = true;
			errors.rejectValue("purchaseBill.tallySetId", "", "请选择『当前开单帐套』");
		}

		if (!hasError
				&& (bean.getOldId() != 0L || bean.getCodeRule()
						.getGenerateRule().equals("MANUAL"))) {
			if (form.getBillCode() != null
					&& !form.getBillCode().trim().equals("")) {
				PurchaseBill hasItems = this.purchaseBillDao.get(form
						.getBillCode());
				if (hasItems != null && hasItems.getId() != form.getId()) {
					errors.rejectValue("purchaseBill.billCode", "", "『编号』已经存在");
					hasError = true;
				}
			} else {
				errors.rejectValue("purchaseBill.billCode", "", "请输入『编号』");
				hasError = true;
			}
		}

		if (!hasError) {
			try {
				SimpleDateFormat arg16 = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				form.setPurchaseDate(arg16.parse(bean.getPurchaseDateStr()
						+ " " + bean.getPurchaseDateHour() + ":"
						+ bean.getPurchaseDateMinute() + ":00"));
			} catch (Exception arg12) {
				hasError = true;
				errors.rejectValue("purchaseDateStr", "",
						"『日期』填写错误，请按以下格式输入：2008-01-01");
			}
		}

		if (!hasError) {
			if (form.getStorehouseId() == 0L) {
				errors.rejectValue("purchaseBill.storehouseId", "", "请选择『仓库』");
				hasError = true;
			} else if (form.getProviderId() == 0L) {
				errors.rejectValue("purchaseBill.providerId", "", "请选择『供应商』");
				hasError = true;
			} else if (form.getManagerId() == 0L) {
				errors.rejectValue("purchaseBill.managerId", "", "请选择『经办人』");
				hasError = true;
			}
		}

		if (!hasError) {
			try {
				form.setImprest(new BigDecimal(bean.getImprestStr()));
			} catch (Exception arg11) {
				hasError = true;
				errors.rejectValue("imprestStr", "", "『预付款』只能输入数字");
			}
		}

		if (!hasError && form.getImprest().compareTo(new BigDecimal(0)) != 0
				&& form.getBankAccountId() == 0L) {
			hasError = true;
			errors.rejectValue("purchaseBill.bankAccountId", "", "请选择『银行账户』");
		}

		if (!hasError && form.getContent() != null
				&& form.getContent().length() >= 2000) {
			hasError = true;
			errors.rejectValue("purchaseBill.content", "", "『摘要』不能超过2000个字符");
		}

		if (!hasError && form.getMark() != null
				&& form.getMark().length() >= 250) {
			hasError = true;
			errors.rejectValue("purchaseBill.mark", "", "『备注』不能超过250个字符");
		}

		boolean arg17 = false;
		BigDecimal totalCost = new BigDecimal(0);
		if (!hasError) {
			for (int i = 0; i < bean.getItems().size(); ++i) {
				PurchaseBillItemBean cb = (PurchaseBillItemBean) bean
						.getItems().get(i);
				PurchaseBillItem c = cb.getPurchaseBillItem();
				if (!cb.getStatus().equals("DELETE")) {
					try {
						c.setAmount(new BigDecimal(cb.getAmountStr()));
					} catch (Exception arg15) {
						hasError = true;
						errors.rejectValue("items", "", "明细的『数量』只能输入数字");
						break;
					}

					try {
						c.setUnitPrice(new BigDecimal(cb.getUnitPriceStr()));
					} catch (Exception arg14) {
						hasError = true;
						errors.rejectValue("items", "", "明细的『单价』只能输入数字");
						break;
					}

					try {
						c.setCost(new BigDecimal(cb.getCostStr()));
					} catch (Exception arg13) {
						hasError = true;
						errors.rejectValue("items", "", "明细的『金额』只能输入数字");
						break;
					}

					totalCost = totalCost.add(c.getCost());
					boolean hasInput = false;
					if (c.getAmount().compareTo(new BigDecimal(0)) != 0
							|| c.getUnitPrice().compareTo(new BigDecimal(0)) != 0
							|| c.getProductId() != 0L || c.getMark() != null
							&& !c.getMark().equals("")) {
						hasInput = true;
					}

					if (hasInput) {
						if (c.getProductId() == 0L) {
							hasError = true;
							errors.rejectValue("items", "", "请选择明细的『产品名称』");
							break;
						}

						if (c.getAmount().compareTo(new BigDecimal(0)) == 0) {
							hasError = true;
							errors.rejectValue("items", "", "明细的『数量』不能为0");
							break;
						}

						arg17 = true;
					} else {
						cb.setStatus("DELETE");
					}
				}
			}
		}

		if (!hasError && !arg17) {
			hasError = true;
			errors.rejectValue("items", "", "明细不能为空");
		}

		if (!hasError && totalCost.compareTo(form.getCost()) != 0) {
			hasError = true;
			errors.rejectValue("items", "", "『总金额』与明细项总金额不符");
		}

	}

	public PurchaseBillDao getPurchaseBillDao() {
		return this.purchaseBillDao;
	}

	public void setPurchaseBillDao(PurchaseBillDao purchaseBillDao) {
		this.purchaseBillDao = purchaseBillDao;
	}
}